/**
 * @Description:
 * @author 若琳
 * @date 2021/7/16
*/

const express = require('express');
const MenuController = express.Router();
const MenuService = require("../../../../../../../../ruoling-system/src/com/ruoling/system/service/v1/system/service.menu.js");
const CommonService = require("../../../../../../../../ruoling-system/src/com/ruoling/system/service/v1/common/service.common.js");
//数据集合
const ResultJson = require("../../../../../../../../ruoling-model/src/com/ruoling/model/SystemModel/ResultJson.js").V("v1.0.0");

const LayuiGrid = require("../../../../../../../../ruoling-model/src/com/ruoling/model/SystemModel/layuiGrid.js");
/**
 * 菜单列表视图
 */
MenuController.get("/system/menu/list/view",function (Request,response){

    response.type(".html");
    response.render("admin/system/sysmenu/menuview.html");
    return;
});

/**
 * 菜单添加视图
 */
MenuController.get("/system/menu/add/view",function (Request,response){

    response.type(".html");
    response.render("admin/system/sysmenu/menuaddview.html");
});

/**
 * 菜单编辑视图
 */
MenuController.get("/system/menu/edit/view",function (Request,response){

    let Id = Request.query.Id;
    MenuService.MenuEditDataService(Id,(err,data)=>{
        console.log(data)
        response.type(".html");
        response.render("admin/system/sysmenu/menueditview.html",{data:data.data,sys_parent_data:data.sys_parent_data});
    });

});


/**
 * 菜单树视图
 */
MenuController.get("/system/menu/tree/view",function (Request,response){

    response.type(".html");
    response.render("admin/system/sysmenu/menutreeview.html");
});

/**
 * 菜单图标视图
 */
MenuController.get("/system/menu/icon/view",function (Request,response){

    MenuService.MenuIconService((err,data)=>{
        console.log(data)
        let arr = [];
        for(let i = 0;i<data.length;i++){
            arr.push(data[i].sys_dict_value);
        }
        response.type(".html");
        response.render("admin/system/sysmenu/menuiconview.html",{data:arr});
    })

});


/**
 * 菜单列表
 */
MenuController.get("/v1/api/system/menu/list",function (Request,response){

    try{
        MenuService.MenuListService((err,data)=>{
            if(err){
                response.send(ResultJson.GUBError());
                return;
            }
                response.send(new LayuiGrid(0,data));
                return;
        });

    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});


/**
 * 菜单添加
 */
MenuController.post("/v1/api/system/menu/add",function (Request,response){

    try{
        let {sys_menu_name,sys_menu_type,sys_parent_id,sys_permission_url,sys_order_num,sys_menu_icon} = Request.body;
        MenuService.MenuAddService(sys_menu_name,sys_menu_type,sys_parent_id,sys_permission_url,sys_order_num,sys_menu_icon,(err,data)=>{
            if(err){
                response.send(ResultJson.GUBError());
                return;
            }
                response.send(ResultJson.MegOk("菜单添加成功"));
                return;
        });
    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});


/**
 * 菜单树
 */
MenuController.get("/v1/api/system/menu/tree",function (Request,response){

    try{
        MenuService.MenuTreeService((err,data)=>{
            if(err){
                response.send(ResultJson.GUBError());
                return;
            }
            response.send(new LayuiGrid(0,data));
            return;
        });
    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});

/**
 * 菜单删除
 */
MenuController.post("/v1/api/system/menu/delete",function (Request,response){

    try{
        let Id = Request.body.Id;
        CommonService.Common_Delete_All("rl_sys_menu_permisson",Id,(err,row)=>{
            if(err){
                response.send(ResultJson.GUBError());
                return;
            }
                response.send(ResultJson.MegOk(""));
                return;
        });
    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});

/**
 * 菜单修改
 */
MenuController.post("/v1/api/system/menu/edit",function (Request,response){

    try{
        let {sys_menu_name,sys_menu_type,sys_parent_id,sys_permission_url,sys_order_num,sys_menu_icon,ID} = Request.body;
        MenuService.MenuEditService(sys_menu_name,sys_menu_type,sys_parent_id,sys_permission_url,sys_order_num,sys_menu_icon,ID,(err,data)=>{
            if(err){
                response.send(ResultJson.GUBError());
                return;
            }
            response.send(ResultJson.MegOk("菜单修改成功"));
            return;
        });
    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});


module.exports = MenuController;
